package org.kardigen.lg.admin.client.training;

import java.util.ArrayList;
import java.util.List;

import org.kardigen.lg.admin.client.training.event.WordSelectionEvent;
import org.kardigen.lg.service.client.proxy.TrainingChapterProxy;
import org.kardigen.lg.service.client.proxy.TrainingProxy;
import org.kardigen.lg.service.client.requestfactory.TrainingRequestContext;
import org.kardigen.lg.service.client.requestfactory.TrainingRequestFactory;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.Window;

public class ChapterEditorViewPresenter implements ChapterEditorViewDisplay.Presenter {

	private final ChapterEditorViewDisplay display;
	private EventBus eventBus;
	private TrainingRequestFactory requestFactory;
	private TrainingRequestContext requestContext;
	private TrainingChapterProxy model;
	private TrainingProxy editableParent;
	private TrainingProxy parent;
	private TrainingChapterProxy trainingProxy;

	public ChapterEditorViewPresenter(ChapterEditorViewDisplay display) {
		this.display = display;
		display.setPresenter(this);

	}

	public void onSave() {
		String value = display.getChapterName().getValue();
		if (value != null && !value.trim().isEmpty()) {

			final List<TrainingChapterProxy> chapters = editableParent.getChapters();
			int idx = 0;
			for (; idx < chapters.size(); idx++) {
				if (model.equals(chapters.get(idx))) {
					break;
				}
			}

			final int index = idx;

			model.setChapterName(value);

			requestContext.saveTraining(editableParent)
			.with("chapters", "chapters.words")
			.fire(new Receiver<TrainingProxy>() {

				@Override
				public void onSuccess(TrainingProxy response) {
					if (response == null) {
						Window.alert("Cannot save chapter!");
					} else {
						Window.alert("Chapter saved!");
						
						parent = response;
						trainingProxy = response.getChapters().get(index);
						
						requestContext = requestFactory.createTrainingRequestContext();
						editableParent = requestContext.edit(response);
						model = requestContext.edit(chapters.get(index));
					}
				}
			});
		}
	}

	public void start(TrainingProxy parent, TrainingChapterProxy trainingProxy, EventBus eventBus) {
		this.parent = parent;
		this.trainingProxy = trainingProxy;
		this.eventBus = eventBus;
		initializeRequestFactory();
		requestContext = requestFactory.createTrainingRequestContext();

		display.getChapterName().setValue("");

		editableParent = requestContext.edit(parent);
		List<TrainingChapterProxy> chapters = editableParent.getChapters();
		if (chapters == null) {
			chapters = new ArrayList<TrainingChapterProxy>();
			editableParent.setChapters(chapters);
		}

		if (trainingProxy == null) {
			model = requestContext.create(TrainingChapterProxy.class);
			chapters.add(model);
			display.getLabel().setText("Creating new chapter");
		} else {
			int idx = parent.getChapters().indexOf(trainingProxy);
			model = requestContext.edit(chapters.get(idx));
			display.getLabel().setText("Editing chapter: " + trainingProxy.getChapterName());
		}
	}

	private void initializeRequestFactory() {
		if (requestFactory == null) {
			requestFactory = GWT.create(TrainingRequestFactory.class);
			requestFactory.initialize(eventBus);
		}
	}

	public void onAddWord() {
		eventBus.fireEvent(new WordSelectionEvent(parent, trainingProxy, null));
   }

}
